define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    require('../su/accordion/oaTabCtrl');
    

    require('oa/js/approval/approvalContentCtrl');
    require('oa/js/approval/approvalCurtainCtrl');
    require('oa/js/approval/dialogs/createDialogCtrl');
    require('oa/js/approval/dialogs/exportDialogCtrl');
    require('oa/js/approval/dialogs/updateDialogCtrl');

    $.widget('rk.approvalPanelCtrl', $.rk.oaPanelCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            var html = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel.tpl'), {});
            elem.html(html);
            me._initAccordion();
            elem.on('click', 'div[applyId]', function () {
                me._handleRowClick($(this));
            });
            elem.find('a[act="searchApproval"]').click(function (event) {
                me._handleSearchClick($(this), event);
            });
            elem.find('a[act="exportApproval"]').click(function (event) {
                me._handleExportClick($(this), event);
            });
            elem.on('click','.oa-close-panel',function(){
                rk.oa.closePanel();
            });
            var header = me.getPanelHeader();
            header.find('a[act="createApproval"]').click($.proxy(me._handleCreateClick, me));
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var titleBar = elem.find('div.oa-head-toolbar');
            var navMore = titleBar.find('.nav-more').parent();
            var navMenu = titleBar.find('.drop_div');
            navMore.rkEasyMenu({
                menu: navMenu
            });
        },
        _loadData: function (viewType, applyStatus, pageNo) {
            var me = this;
            var elem = me.element;
            var url = '/json/oa_approval/my-submit-list.action';
            if (viewType == 2) {
                url = '/json/oa_approval/submit-me-list.action';
            } else if (viewType == 3) {
                url = '/json/oa_approval/all-approval-list.action';
            }
            if(pageNo == null){
                pageNo = 1;
            }
            //加载中
            if (viewType == 1) {
                if (applyStatus == 1) {
                    $('#my_submit_content').find('div[idx=0]').beLoading();
                } else if (applyStatus == 2) {
                    $('#my_submit_content').find('div[idx=1]').beLoading();
                } else if (applyStatus == 3) {
                    $('#my_submit_content').find('div[idx=2]').beLoading();
                }
            } else if (viewType == 2) {
                if (applyStatus == 1) {
                    $('#submit_me_content').find('div[idx=0]').beLoading();
                } else if (applyStatus == 2) {
                    $('#submit_me_content').find('div[idx=1]').beLoading();
                } else if (applyStatus == 3) {
                    $('#submit_me_content').find('div[idx=2]').beLoading();
                }
            } else if (viewType == 3) {
                $('#all_approvals_content').find('div[idx=0]').beLoading();
            }
            rk.post(url, {
                pageNo: pageNo,
                viewType: viewType,
                applyStatus: applyStatus
            }).done(function (json) {
                if (json.status == 0) {
                    $.each(json.data.users, function (i, o) {
                        if (o.icon == '') {
                            o.icon = DEFAULTS.USER_DEFAULT_ICON;
                        }
                    });
                    me._renderPanel(viewType, applyStatus, json.data);
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _renderPanel: function (viewType, applyStatus, data) {
            var me = this;
            var elem = me.element;
            var listHtml;
            var $approvalPagination;
            if (viewType == 1) {
                if (data.mySubmitEndCount > 0) {
                    $('#mySubmitEndCount').html('('+data.mySubmitEndCount+')');
                }
                if (data.totalCount - 0 > 0) {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_my_submit.tpl'), data);
                } else {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_default_list.tpl'), {});
                }
                if(applyStatus == 1){
                    $('#my_submit_content').find('div[idx=0]').html(listHtml);
                } else if(applyStatus == 2){
                    $('#my_submit_content').find('div[idx=1]').html(listHtml);
                } else if(applyStatus == 3){
                    $('#my_submit_content').find('div[idx=2]').html(listHtml);
                }
                $approvalPagination = $('div.my_submit_pagination');
            } else if (viewType == 2) {
                if (data.waitMeApprovalCount > 0) {
                    $('#waitMeApprovalCount').html('('+data.waitMeApprovalCount+')');
                }else{
                    $('#waitMeApprovalCount').html('');
                }
                if (data.unReadCopyCount > 0) {
                    $('#unReadCopyCount').html('('+data.unReadCopyCount+')');
                }
                if (data.totalCount - 0 > 0) {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_submit_me.tpl'), data);
                } else {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_default_list.tpl'), {});
                }
                if(applyStatus == 1){
                    $('#submit_me_content').find('div[idx=0]').html(listHtml);
                } else if(applyStatus == 2){
                    $('#submit_me_content').find('div[idx=1]').html(listHtml);
                } else if(applyStatus == 3){
                    $('#submit_me_content').find('div[idx=2]').html(listHtml);
                }
                $approvalPagination = $('div.submit_me_pagination');
            } else if (viewType == 3) {
                if (data.totalCount - 0 > 0) {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_all_approval.tpl'), data);
                } else {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_default_list.tpl'), {});
                }
                $('#all_approvals_content').find('div[idx=0]').html(listHtml);
                $approvalPagination = $('div.all_approvals_pagination');
            }
            $approvalPagination.rkPaging({
                hideForward: 1,
                pagenum: data.pageNo - 0,
                pagesize: data.pageSize - 0,
                rowscount: data.totalCount - 0,
                onChange: function(pageNo) {
                    me._loadData(viewType, applyStatus, pageNo);
                }
            });
        },
        _initAccordion: function () {
            var me = this;
            var elem = me.element;
            var content = me.getPanelContent();
            me.accordion = content.find('div.approval-accordion:first');
            rk.post('/json/oa_approval/approval-index.action', {}).done(function (json) {
                if (json.status == 0) {
                    if (json.data['canViewAllFlag'] == 0) {
                        $('#all_approvals_head').remove();
                        $('#all_approvals_content').remove();
                    } else {
                        $('#all_approvals_head').show();
                        $('#all_approvals_content').show();
                    }
                    me.accordion.rkAccordion({
                        onOpen: function (e, ui) {
                            me._onAccordionOpen(e, ui, json.data);
                        }
                    });
                    if (json.data.waitCount > 0) {
                        $('#waitCountSpan').html(json.data.waitCount);
                        $('#waitCountSpan').show();
                    }
                    $('span[act=searchIconSpan]').beTip({dir:'left'});
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _onAccordionOpen: function(e, ui, data){
            var me = this;
            var elem = me.element;
            var subPanel = ui.panel;
            var panelIndex = ui.index;
            if(subPanel.attr('inited') != 'true') {
                if (panelIndex === 0) {
                    subPanel.oaTabCtrl({
                        tabs: [{
                            title: rk.i18n('APPROVAL_STATUS_WAIT'),
                            html_attrs: {
                                viewType: '1',
                                applyStatus: '1'
                            }
                        }, {
                            title: rk.i18n('APPROVAL_STATUS_END') + '<span id="mySubmitEndCount"></span>',
                            html_attrs: {
                                id: 'mySubmitEndA',
                                viewType: '1',
                                applyStatus: '2'
                            }
                        }, {
                            title: rk.i18n('APPROVAL_STATUS_PASS'),
                            html_attrs: {
                                viewType: '1',
                                applyStatus: '3'
                            }
                        }],
                        onTabSelect: function (e, info) {
                            me._onTabSelect(subPanel, info);
                        }
                    });
                } else if (panelIndex === 1) {
                    subPanel.oaTabCtrl({
                        tabs: [{
                            title: rk.i18n('APPROVAL_RESULT_WAIT') + '<span id="waitMeApprovalCount"></span>',
                            html_attrs: {
                                viewType: '2',
                                applyStatus: '1'
                            }
                        }, {
                            title: rk.i18n('APPROVAL_RESULT_FINISH'),
                            html_attrs: {
                                viewType: '2',
                                applyStatus: '2'
                            }
                        }, {
                            title: rk.i18n('APPROVAL_TYPE_COPY') + '<span id="unReadCopyCount"></span>',
                            html_attrs: {
                                viewType: '2',
                                applyStatus: '3'
                            }
                        }],
                        onTabSelect: function (e, info) {
                            me._onTabSelect(subPanel, info);
                        }
                    });
                } else if (panelIndex === 2) {
                    subPanel.oaTabCtrl({
                        tabs: [{
                            title: rk.i18n('APPROVAL_TYPE_ALL'),
                            html_attrs: {
                                viewType: '3',
                                applyStatus: '1'
                            }
                        }],
                        onTabSelect: function (e, info) {
                            me._onTabSelect(subPanel, info);
                        }
                    });
                }
            }
            subPanel.oaTabCtrl('select', 0);
            subPanel.attr('inited', 'true');
        },
        _onTabSelect: function(subPanel, info){
            var me = this;
            var elem = me.element;
            var tabBtn = info.tabBtn;
            var index = info.index;
            var child = info.child;
            var content = me.getPanelContent();
            var accordion = content.find('div.approval-accordion:first');
            accordion.find('div.oa-head-toolbar').hide();
            var o = subPanel.prev().find('.oa-head-toolbar');
            o.show();
            var elem = subPanel.find('div.oa-accordion-toolbar');
            me._loadData(tabBtn.attr('viewType'), tabBtn.attr('applyStatus'));
        },
        _handleRowClick: function (row) {
            var me = this;
            var elem = me.element;
            var applyId = row.attr('applyId');
            var approvalUserId = row.attr('approvalUserId');
            $('div[applyId]').removeClass('show-content-hover');
            row.addClass('show-content-hover');
            me.popupContent({
                onSuccess: function (contentElem) {
                    contentElem.approvalContentCtrl({
                        applyId: applyId,
                        approvalUserId: approvalUserId
                    });
                }
            });
        },
        _handleCreateClick: function () {
            var me = this;
            rk.dialog({
                title: rk.i18n('APPROVAL_SUBMIT_NEW'),
                success: function (dialogElem){
                    dialogElem.createApprovalDialogCtrl();
                },
                footer: [{
                    act: 'cancelSubmit', color: 'white', visible: true, text: rk.i18n('CANCEL')
                }, {
                    act: 'submitApproval', visible: true, text:  rk.i18n('APPROVAL_NEXT_STEP')
                }]
            });
        },
        _handleSearchClick: function (searchA, event) {
            event = event || window.event;
            if(event.stopPropagation) { //W3C阻止冒泡方法
                event.stopPropagation();
            } else {
                event.cancelBubble = true; //IE阻止冒泡方法
            }
            rk.oa.popupCurtain({
                onSuccess: function (curtainElem) {
                    curtainElem.approvalCurtainCtrl({
                        viewType: searchA.attr('viewType')
                    });
                }
            });
        },
        _handleExportClick: function (exportA, event) {
            event = event || window.event;
            if(event.stopPropagation) { //W3C阻止冒泡方法
                event.stopPropagation();
            } else {
                event.cancelBubble = true; //IE阻止冒泡方法
            }
            rk.dialog({
                title: rk.i18n('APPROVAL_EXPORT'),
                success: function (dialogElem){
                    dialogElem.exportApprovalDialogCtrl({
                        viewType: exportA.attr('viewType')
                    });
                },
                footer: ['ok', 'cancel']
            });
        }
    });

    return $;
});